package com.api.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.api.entity.Modules;
import com.api.entity.Result;
import com.api.service.ModulesService;
import com.api.service.RoleModulesService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

@Api("Tree 树")
@RestController
/* @RequestMapping("/tree") */
@ApiIgnore
public class ModulesController {

	@Autowired
	private ModulesService mService;
	@Autowired
	private RoleModulesService roleModulesService;
	@ApiOperation("查询所有 Tree")
	@PostMapping("/GetAllModuleLayUI")
	public Object queryAllModules(HttpServletRequest request,@ApiParam("Token 密钥")@RequestParam("token") String token) {
		
		String sessionToken = (String) request.getSession().getAttribute("token");
		if(sessionToken==""||sessionToken==null) {
			Result result = new Result("您的Token已过期，请重新获取并登录",0,false);
			return result;
		}else if(sessionToken.equals(token)) {
			return mService.loadTree();
		}else {
			Result result = new Result("请核实 Token 是否正确","",false);
			return result;
		}
	}
	@ApiOperation("查询指定角色名称下的 Tree")
	@PostMapping("/GetSysRightsJsonLayUI")
	public Object findAllByRname(@ApiParam("角色名称")@RequestParam("roleNames[]")String[] roleNames,HttpServletRequest request,@ApiParam("Token 密钥")@RequestParam("token") String token) {
		
		String sessionToken = (String) request.getSession().getAttribute("token");
		if(sessionToken==""||sessionToken==null) {
			Result result = new Result("您的Token已过期，请重新获取并登录",0,false);
			return result;
		}else if(sessionToken.equals(token)) {
			return mService.loadTreeByRname(roleNames);
		}else {
			Result result = new Result("请核实 Token 是否正确","",false);
			return result;
		}
	}
	@ApiOperation("创建模块")
	@PostMapping("/CreateModule")
	public Object addModules(@ApiParam("模块名")@RequestParam(value="name") String name,@ApiParam("父模块id")@RequestParam(value="parentId") int parentId,@ApiParam("模块路径")@RequestParam(value="path") String path,@ApiParam("权重")@RequestParam(value="weight") int weight,HttpServletRequest request,@ApiParam("Token 密钥")@RequestParam("token") String token) {
		
		String sessionToken = (String) request.getSession().getAttribute("token");
		if(sessionToken==""||sessionToken==null) {
			Result result = new Result("您的Token已过期，请重新获取并登录",0,false);
			return result;
		}else if(sessionToken.equals(token)) {
			Modules modules = new Modules();
			modules.setName(name);
			modules.setParentId(parentId);
			modules.setPath(path);
			modules.setWeight(weight);
			int i = mService.addModules(modules);
			if(i>0) {
				Result result = new Result("添加成功","",true);
				return result;
			}else {
				Result result = new Result("添加失败","",false);
				return result;
			}
		}else {
			Result result = new Result("请核实 Token 是否正确","",false);
			return result;
		}
	}
	@ApiOperation("删除模块")
	@PostMapping("/DeleteModule")
	public Object deleteModules(@ApiParam("模块id")@RequestParam("mId") int mId,HttpServletRequest request,@ApiParam("Token 密钥")@RequestParam("token") String token) {
		
		String sessionToken = (String) request.getSession().getAttribute("token");
		if(sessionToken==""||sessionToken==null) {
			Result result = new Result("您的Token已过期，请重新获取并登录",0,false);
			return result;
		}else if(sessionToken.equals(token)) {
			int i = mService.deleteModules(mId);
			if(i>0) {
				roleModulesService.deleteAllModulesByid(String.valueOf(mId));
				Result result = new Result("删除成功","",true);
				return result;
			}else {
				Result result = new Result("删除失败","",false);
				return result;
			}
		}else {
			Result result = new Result("请核实 Token 是否正确","",false);
			return result;
		}
	}
	@ApiOperation("根据模块id,查询模块信息")
	@PostMapping("/GetModuleById")
	public Object findModulesByid(@ApiParam("模块id")@RequestParam("id") int id,HttpServletRequest request,@ApiParam("Token 密钥")@RequestParam("token") String token) {
		
		String sessionToken = (String) request.getSession().getAttribute("token");
		if(sessionToken==""||sessionToken==null) {
			Result result = new Result("您的Token已过期，请重新获取并登录",0,false);
			return result;
		}else if(sessionToken.equals(token)) {
			Modules mod = mService.findModulesByid(id);
			if(mod!=null) {
				Result result = new Result("",mod,true);
				return result;
			}else {
				Result result = new Result("","",false);
				return result;
			}
		}else {
			Result result = new Result("请核实 Token 是否正确","",false);
			return result;
		}
	}
	@ApiOperation("更新模块")
	@PostMapping("/UpdateModule")
	public Object updateModulesBymid(@ApiParam("模块名")@RequestParam(value = "name", required = false) String name,@ApiParam("父模块id")@RequestParam(value = "parentId", required = false) int parentId,@ApiParam("模块路径")@RequestParam(value = "path", required = false) String path,@ApiParam("权重")@RequestParam(value = "weight", required = false) int weight,@ApiParam("模块id")@RequestParam(value = "mId", required = false) int mId,HttpServletRequest request,@ApiParam("Token 密钥")@RequestParam(value = "token", required = false) String token) {
		
		String sessionToken = (String) request.getSession().getAttribute("token");
		if(sessionToken==""||sessionToken==null) {
			Result result = new Result("您的Token已过期，请重新获取并登录",0,false);
			return result;
		}else if(sessionToken.equals(token)) {
			Modules modules = new Modules();
			modules.setName(name);
			modules.setParentId(parentId);
			modules.setPath(path);
			modules.setWeight(weight);
			modules.setId(mId);
			int i = mService.updateModulesBymid(modules);
			if(i>0) {
				Result result = new Result("修改成功","",true);
				return result;
			}else {
				Result result = new Result("修改失败","",false);
				return result;
			}
		}else {
			Result result = new Result("请核实 Token 是否正确","",false);
			return result;
		}
	}
	
}
